import Vue from 'vue'
import VueRouter from 'vue-router'
import LoginOne from '../components/loginOne.vue'
import Home from '../components/home.vue'
import Welcome from '../components/welcome.vue'
import Users from '../components/user/users.vue'
import Rights from '../components/power/Rights.vue'
import Roles from '../components/power/Roles.vue'
import Cate from '../components/goods/Cate.vue'
import Report from '../components/Report/report.vue'
Vue.use(VueRouter)

const routes = [
  {path:'/',redirect:"/loginOne"},
  {path:'/loginOne',component:LoginOne},
  {path:'/home',component:Home,redirect:'/welcome',
  children:[
    {path:'/welcome',component:Welcome},
    {path:'/users',component:Users},
    {path:'/rights',component:Rights},
    {path:'/roles',component:Roles},
    {path:'/categories',component:Cate},
    {path:'/reports',component:Report}
  ]
  
},
]


const router = new VueRouter({
  routes
}) 
// 挂在路由导航守卫
router.beforeEach((to,from,next)=>{
  // to将要访问的路径
  // from从哪个路径跳转而来
  // next 是一个函数，表示放行
  // next()放行  next("/login") 强制跳转
  if(to.path==="/loginOne") return next();
  //获取token
  const tokenStr = window.sessionStorage.getItem("token");
  if(!tokenStr) return next('/loginOne');
  next();
})
export default router
